﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;
using MyCommerce.Presentation.Stores.IViews;
using Innova.FoundationFramework.DataTypes;
using MyCommerce.Business.Stores;

namespace MyCommerce.Presentation.Stores.Presenters
{
    public class ProductPriceAdministrationPresenter
    {
        private IProductPriceAdministration pViewToPresent;

        public ProductPriceAdministrationPresenter(IProductPriceAdministration vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnCreate += new EventHandler(pViewToPresent_OnCreate);
            pViewToPresent.OnDelete += new GenericEventHandler<long>(pViewToPresent_OnDelete);
            pViewToPresent.OnEdit += new GenericEventHandler<ProductPrice>(pViewToPresent_OnEdit);
            pViewToPresent.OnLoadView += new GenericEventHandler<long>(pViewToPresent_OnLoadView);
            pViewToPresent.OnChange += new GenericEventHandler<ProductPrice>(pViewToPresent_OnChange);
        }

        void pViewToPresent_OnChange(object sender, GenericEventArgs<ProductPrice> e)
        {
            ProductPriceManager vProductPriceManager = new ProductPriceManager();
            vProductPriceManager.ChangeProductPrice(pViewToPresent.ObjItemSelected);
            LoadProductList();
        }

        void pViewToPresent_OnDelete(object sender, GenericEventArgs<long> e)
        {
            try
            {
                ProductManager vProductManager = new ProductManager();
                vProductManager.DeleteProduct(e.EventData);
                LoadProductList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void LoadProductList()
        {
            ProductPriceManager vProductPriceManager = new ProductPriceManager();
            pViewToPresent.ColItemsList = vProductPriceManager.GetProductPrices();
        }

        private void pViewToPresent_OnLoadView(object sender, GenericEventArgs<long> e)
        {
            LoadProductList();
        }

        private void pViewToPresent_OnEdit(object sender, GenericEventArgs<ProductPrice> e)
        {
            bool result = pViewToPresent.InvokeEditionView(e.EventData);
            if (result)
                LoadProductList();
        }

        private void pViewToPresent_OnCreate(object sender, EventArgs e)
        {
            bool result = pViewToPresent.InvokeEditionView(null);
            if (result)
                LoadProductList();
        }
    }
}
